package com.h.blog.controller;

import com.h.blog.pojo.dataobject.BlogInfo;
import com.h.blog.pojo.request.AddBlogRequest;
import com.h.blog.pojo.request.UpdateBlogRequest;
import com.h.blog.pojo.response.BlogInfoResponse;
import com.h.blog.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfoResponse> getList() {
        log.info("获取博客列表");
        List<BlogInfoResponse> blogInfos = blogService.getList();
        return blogInfos;
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfoResponse getBlogDetail(@NotNull(message = "blogId不能为空") Integer blogId) {
        log.info("获取博客详情 blogId: " + blogId);
        return blogService.getBlogDetail(blogId);
    }

    @RequestMapping("/add")
    public Boolean add(@RequestBody @Validated AddBlogRequest addBlogRequest) {
        log.info("添加博客的userId: " + addBlogRequest.getUserId() + "博客的标题是: " + addBlogRequest.getTitle());
        return blogService.add(addBlogRequest);
    }

    @RequestMapping("/update")
    public Boolean update(@RequestBody @Validated UpdateBlogRequest updateBlogRequest) {
        log.info("修改博客的blogId: " + updateBlogRequest.getId());
        return blogService.update(updateBlogRequest);
    }

    @RequestMapping("/delete")
    public Boolean delete(@NotNull(message = "blogId不能为空") Integer blogId) {
        log.info("删除博客的blogId: " + blogId);
        return blogService.delete(blogId);
    }
}
